package com.iflysse.dao;

import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.req.AddResourceToChapterPoReq;
import com.iflysse.pojo.po.req.ChapterIdPoReq;
import com.iflysse.pojo.po.req.ResourceIdPoReq;
import com.iflysse.pojo.po.req.StudentLearnRecordPoReq;
import com.iflysse.pojo.po.resp.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * 资源dao
 */
@Repository
public interface ResourceDao {
    /**
     * 将资源绑定到章
     * @param addResourceToChapterPoReq
     * @return
     */
    @Insert({
            "insert into tb_chapter_resource(chapter_resource_id,chapter_id,resource_id,resource_type,resource_type_name,resource_title)" +
                    " values(UUID(),#{chapterId},#{resourceId},#{resourceType},#{resourceTypeName},#{resourceTitle})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "chapter_resource_id")
    int addResourceToChapter(AddResourceToChapterPoReq addResourceToChapterPoReq) throws SQLException;


    /**
     * 根据章编号获取资源列表
     * @param chapterIdPoReq
     * @return
     */
    @Select({"select resource_id , resource_index,resource_title ,resource_type from tb_chapter_resource where chapter_id = #{chapterId} and is_delete = 0"})
    @Results({
            @Result(property = "resourceId",column = "resource_id"),
            @Result(property = "resourceIndex",column = "resource_index"),
            @Result(property = "resourceTitle",column = "resource_title"),
            @Result(property = "resourceType",column = "resource_type"),
    })
    List<ResourcePoResp> getResourceByChapterId(ChapterIdPoReq chapterIdPoReq) throws SQLException;


    /**
     * 根据资源编号获取html资源详情
     * @param resourceIdPoReq
     * @return
     */

    @Select({"select * from tb_resource_html where resource_id = #{resouceId}"})
    @Results({
            @Result(property = "resourceContent",column = "resource_content"),
    })
    ResourceHtmlPoResp getHtmlResourceByResourceId(ResourceIdPoReq resourceIdPoReq) throws SQLException;

    /**
     * 根据资源编号获取文档和视频资源详情
     * @param resourceIdPoReq
     * @return
     */
    @Select({"select doc_path  from tb_resource_doc where resource_id = #{resouceId}"})
    @Results({
            @Result(property = "resourcePath",column = "doc_path"),
    })
    ResourceDocAndVideoPoResp getDocAndVideoResourceByResourceId(ResourceIdPoReq resourceIdPoReq) throws SQLException;


    /**
     * 根据资源编号获取文档和视频资源详情情况2
     * @param resourceIdPoReq
     * @return
     */
    @Select({"select video_path  from tb_resource_video where resource_id =  #{resouceId}}"})
    @Results({
            @Result(property = "resourcePath",column = "video_path"),
    })
    ResourceDocAndVideoPoResp getDocAndVideoResourceByResourceId2(ResourceIdPoReq resourceIdPoReq) throws SQLException;

    /**
     * 根据资源编号获取测试问题详情
     * @param resourceIdPoReq
     * @return
     * @throws SQLException
     */
    @Select({"select question_id  from tb_resource_test where resource_id = #{resouceId}"})
    @Results({
            @Result(property = "questionId",column = "question_id"),
    })
    QuestionPoResp getTestResourceByResourceId(ResourceIdPoReq resourceIdPoReq) throws SQLException;


    /**
     * 根据资源编号和学生编号获取学生的学习某个资源的记录数量,
     * 用来判断学习记录的更新
     * @param taskId 任务编号编号
     * @param resourceId 资源编号
     * @param studentId  学生编号
     * @return
     */
    @Select({"select count(*) from tb_task_learn_record where resource_id = #{resourceId}  and student_id = #{studentId} and task_id = #{taskId}"})
    @Results({
            @Result(property = "questionId",column = "question_id"),
    })
    int getStudentLearnRecordByResourceIdAndStudentId(String taskId,String resourceId,String studentId) throws SQLException;

    /**
     * 添加学生学习记录
     * @param studentLearnRecordPoReq
     * @return
     */
    @Insert({"insert into tb_task_learn_record(task_learn_record_id,task_id,resource_id,resource_type,student_id,chapter_id) values(UUID(),#{taskId},#{resourceId},#{resourceType},#{studentId},#{chapterId})"})
    @Options(useGeneratedKeys = true, keyProperty = "task_learn_record_id")
    int  addStudentLearnRecord(StudentLearnRecordPoReq studentLearnRecordPoReq) throws SQLException;

    /**
     * 根据关键词获取资源信息
     * @param keywords 关键字
     * @return
     */
    @Select({"select * from tb_chapter_resource where UPPER(resource_title) like  BINARY CONCAT('%',UPPER('${keywords}'),'%')"})
    @Results({
            @Result(property = "resourceId",column = "resource_id"),
            @Result(property = "resourceTitle",column = "resource_title"),
            @Result(property = "resourceType",column = "resource_type"),
    })
    List<SearchResourcePoResp> getSearchResourceByKeywords(String keywords) throws SQLException;


    @Select({" <script>" +
            "select * from tb_chapter_resource where resource_id in " +
            " <foreach collection='resourceIds' open='(' item='resourceId' separator=',' close=')'> #{resourceId}</foreach> "+
            " </script>" })
    @Results({
            @Result(property = "resourceId",column = "resource_id"),
            @Result(property = "resourceTitle",column = "resource_title"),
            @Result(property = "resourceType",column = "resource_type"),
    })
    List<SearchResourcePoResp> getSearchResourceByResourceIds(List<String> resourceIds) throws SQLException;
}
